require(['./config'],() => {
	require(['template',"request"],(template,req)=>{
		class Login{
			constructor() {
				this.init()
				this.action()
			}
			init(){
				// 拿到数据与进行比较，有就登录成功，没有就跳转注册
				const input1 = $('input[type=email]')
				const input2 = $('input[type=password]')
				const btn = $('.login')
				btn.click(function(){
					const username = input1.val()
					const password = input2.val()
					
					let userList = localStorage.getItem('userList')
					
					if(userList){
						// 有数据的情况
						userList = JSON.parse(userList)
						// 数组遍历
						const res = userList.some(item => {
							return (item.username === username && item.password === password)
						})
						if(res){
							// 保存一条cookie
							const str = JSON.stringify({username,password})
							setCookie('userInfo',str,{
								path:'/'
							})
							
							alert('登录成功！')
							location.href = '../index.html'
						}else{
							alert('用户名或密码错误.....如果未注册，请注册！')
						}
					}else{
						alert('请先注册')
						location.href = '../html/registered.html'
					}
				})							
			}
			action(){
				$('input[type=email]').focus(function(){
					$(this).css('border-color','#0070c9');
					$(this).css('box-shadow','0 0 6px rgba(0,112,201,.5)')
				})
				$('input[type=email]').blur(function(){
					$(this).css('border-color','#ccc');
					$(this).css('box-shadow','none')
				})
				$('input[type=password]').focus(function(){
					$(this).css('border-color','#0070c9');
					$(this).css('box-shadow','0 0 6px rgba(0,112,201,.5)')
				})
				$('input[type=password]').blur(function(){
					$(this).css('border-color','#ccc');
					$(this).css('box-shadow','none')
				})
				
				// 注册
				$('.registered').click(function(){
					$(location).attr('href','../html/registered.html');
				})
			}
			
		}
		return new Login();
	})
})